<?php
class ControllerAccountOrder extends Controller {
	private $error = array();

	/**
	 * @param //1:小计  2:总计   3:折扣   4:秒杀  5:使用券  6:运费 $type
	 */
	//获取订单相关统计信息
	public function getOrderTotalInfo(){
	    if (!$this->actionCheck(true,false)) {
	        return;
	    }
	    $this->load->model('account/order');
	    $data['order_total_info']= $this->model_account_order->getOrderTotalByType($this->request->get['order_id'],$this->request->get['type']);

	    $this->response->setDataResult(1,"成功",$data);
	}

	public function index() {
	    if (!$this->actionCheck(true,false)) {
            return;
        }

		if (isset($this->request->get['page'])) {
			$page = $this->request->get['page'];
		} else {
			$page = 1;
		}

		$data['orders'] = array();

		$this->load->model('account/order');

		$order_total = $this->model_account_order->getCustomerSaleTotalOrders();

		$results = $this->model_account_order->getSaleOrders(($page - 1) * 10, 10);

		foreach ($results as $result) {
			$products = $this->model_account_order->getOrderProducts($result['order_id']);
			$copy_products = array();
			$this->load->model('tool/image');
			foreach ($products as $product){
			   $product['popup_image'] = $this->model_tool_image->resize($product['image'], $this->config->get('config_image_popup_width'), $this->config->get('config_image_popup_height'));
			   $product['thumb_image'] = $this->model_tool_image->resize($product['image'], $this->config->get('config_image_additional_width'), $this->config->get('config_image_additional_height'));
			   array_push($copy_products, $product);
			}

			$products = $copy_products;

			$data['orders'][] = array(
				'order_id'   => $result['order_id'],
				'status'     => $result['status'],
			    'order_status_id'=> $result['order_status_id'],
				'date_added' => date($this->config->get('date_format_long'), strtotime($result['date_added'])),
				'total'      => $result['total'],
			    'firstname'      =>$result['firstname'],
			    'products'    =>$products
			);
		}

		$data['total'] = $order_total;
		$data['pageIndex'] = $page;
		$data['pageSize'] = 10;

		$this->response->setDataResult(1,"",$data);
	}

	public function boughts() {
	    if (!$this->actionCheck(true,false)) {
            return;
        }

		if (isset($this->request->get['page'])) {
			$page = $this->request->get['page'];
		} else {
			$page = 1;
		}

		$data['orders'] = array();

		$this->load->model('account/order');

		$order_total = $this->model_account_order->getTotalOrders();

		$results = $this->model_account_order->getOrders(($page - 1) * 10, 10);

		foreach ($results as $result) {
		    $product_name = "";
		    $time = "";
		    $quantity = 1;
		    $product_id = 0;
			$products = $this->model_account_order->getOrderProducts($result['order_id']);
			foreach ($products as $product) {
			    $product_name = $product['name'];
			    $quantity = $product['quantity'];
			    $product_id = $product['product_id'];
			    $product_time = $this->model_account_order->getOrderTimeOption($result['order_id'],$product['order_product_id']);
			    if($product_time)
			    {
			        $time = $product_time['value'];
			    }
			    break;
			}

			$data['orders'][] = array(
				'order_id'   => $result['order_id'],
				'status'     => $result['status'],
			    'order_status_id'=> $result['order_status_id'],
				'date_added' => date($this->config->get('date_format_long'), strtotime($result['date_added'])),
				'name'   => $product_name,
			    'quantity'   => $quantity,
			    'time'   => $time,
				'total'      => $result['total'],
			    'review_id'      => $result['review_id'],
			    'product_id'      => $product_id
			);
		}

		$data['total'] = $order_total;
		$data['pageIndex'] = $page;
		$data['pageSize'] = 10;

		$this->response->setDataResult(1,"",$data);
	}

	public function sales()
	{
	    if (!$this->actionCheck(true,false)) {
	        return;
	    }

	    if (isset($this->request->get['page'])) {
	        $page = $this->request->get['page'];
	    } else {
	        $page = 1;
	    }

	    $data['orders'] = array();

	    $this->load->model('account/order');
	    $this->load->model('tool/image');

	    $order_total = $this->model_account_order->getSaleTotalOrders();

	    $results = $this->model_account_order->getSaleOrders(($page - 1) * 10, 10);

	    foreach ($results as $result) {
	    	$product_name = "";
	    	$time = "";
	    	$quantity = 1;
			$products = $this->model_account_order->getOrderProducts($result['order_id']);
			$product_id = 0;
			foreach ($products as $product) {
			    $product_name = $product['name'];
			    $quantity = $product['quantity'];
			    $product_time = $this->model_account_order->getOrderTimeOption($result['order_id'],$product['order_product_id']);
			    if($product_time)
			    {
			        $time = $product_time['value'];
			    }
			    $product_id = $product['product_id'];
			    break;
			}

	        $data['orders'][] = array(
	            'order_id'   => $result['order_id'],
	            'customer_name'       => $result['firstname'],
	            'status'     => $result['status'],
	            'order_status_id'=> $result['order_status_id'],
	            'date_added' => date($this->config->get('date_format_long'), strtotime($result['date_added'])),
	            'name'   => $product_name,
	            'quantity'   => $quantity,
	            'time'   => $time,
	            'total'      => $result['total'],
	            'review_id'      => $result['review_id'],
	            'product_id'      => $product_id
	        );
	    }

	    $data['total'] = $order_total;
	    $data['pageIndex'] = $page;
	    $data['pageSize'] = 10;

	    $this->response->setDataResult(1,"",$data);
	}

	public function info() {
		if (!$this->actionCheck(true,false)) {
	        return;
	    }

		if (isset($this->request->get['order_id'])) {
			$order_id = $this->request->get['order_id'];
		} else {
			$order_id = 0;
		}

		$this->load->model('account/order');
		$this->load->model('tool/image');
		$this->load->model('catalog/review');

		$order_info = $this->model_account_order->getOrder($order_id);



		if ($order_info) {

			$products = $this->model_account_order->getOrderProducts($order_info['order_id']);
			$copy_products = array();
			$this->load->model('tool/image');
			foreach ($products as $product){
			   $product['popup_image'] = $this->model_tool_image->resize($product['image'], $this->config->get('config_image_popup_width'), $this->config->get('config_image_popup_height'));
			   $product['thumb_image'] = $this->model_tool_image->resize($product['image'], $this->config->get('config_image_additional_width'), $this->config->get('config_image_additional_height'));
			   array_push($copy_products, $product);
			}

			$products = $copy_products;

			$data['order'] = array(
				'order_id'   => $order_info['order_id'],
				'status'     => $order_info['status'],
			    'order_status_id'=> $order_info['order_status_id'],
				'date_added' => date($this->config->get('date_format_long'), strtotime($order_info['date_added'])),
				'total'      => $order_info['total'],
			    'firstname'  =>$order_info['firstname'],
                'shipping_firstname' =>$order_info['shipping_firstname'],
                'address'    =>$order_info['shipping_country'] .$order_info['shipping_zone']. $order_info['shipping_district'] .$order_info['shipping_address_1'],
			    'payment_method' =>$order_info['payment_method'],
			    'products'    =>$products
			);

            $this->response->setDataResult(1,"",$data);

		} else {
		    $this->response->setDataResult(0,"订单不存在！",null);
		}
	}

	public function getLogisticsByApi()
	{
	    $this->load->model('account/order');

	    $order_logistics = $this->model_account_order->getOrderLogistics($this->request->get['order_id']);


	    if (count($order_logistics)) {
	        $ch = curl_init();
	        $url = 'http://apis.baidu.com/netpopo/express/express1?type='.$order_logistics['logistics_type'].'&number='.$order_logistics['logistics_num'];

	        $header = array(
	            'apikey: fbe5e2bb22bf2f66458b4d44b8df49d0'
	        );
	        // 添加apikey到header
	        curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
	        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
	        // 执行HTTP请求
	        curl_setopt($ch, CURLOPT_URL, $url);
	        $res = curl_exec($ch);
	        curl_close($ch);
	        $this->response->addHeader('Content-Type: application/json');
	        $this->response->setOutput($res);
	    }else{
	        $this->response->addHeader('Content-Type: application/json');
	        $this->response->setOutput(json_encode(array(
	            'status' => '10000',
	            'msg' => '没有相关物流信息'
	        )));
	    }
	}

}